<!-- --------- 实名认证----------- -->
<template>
	<view class="smrz">
		<view class="inputContent">
			<view class="list">
				<view class="leftText">短信验证码：</view>
				<input
					type="password"
					maxlength="6"
					v-model="captcha"
					placeholder="请输入短信验证码"
					placeholder-class="pl"
				/>
				<button
					:style="{
						color: `#2a62ff`,
						margin: `0 0 0 12rpx`,
						padding: 0,
						fontSize: `28rpx`,
						border: `none`,
					}"
					:plain="true"
					@click="getVerificationCode"
				>
					{{ codeBtn.text }}
				</button>
			</view>
			<view class="list">
				<view class="leftText">设置新密码：</view>
				<input
					type="password"
					maxlength="6"
					v-model="newPassword"
					placeholder="请输入新密码"
					placeholder-class="pl"
				/>
			</view>
			<view class="list">
				<view class="leftText">确认新密码</view>
				<input
					maxlength="6"
					placeholder="请再次输入密码"
					v-model="repeatPassword"
					type="password"
					placeholder-class="pl"
				/>
			</view>

			<text :style="{ fontSize: `24rpx`, color: `#666`, marginLeft: `40rpx`, lineHeight: 3 }">
			</text>
		</view>
		<button @click="submit" class="submit">提交</button>
	</view>
</template>

<script>
	import { sleep } from "@/static/utils";
	export default {
		data() {
			return {
				captcha: "",
				newPassword: "",
				repeatPassword: "",

				codeBtn: {
					text: "获取验证码",
					disabled: false,
					count: 60,
				},
			};
		},
		onLoad({ type }) {
			this.type = type;
			uni.setNavigationBarTitle({
				title: { login: "重置登录密码", transaction: "重置交易密码" }[type],
			});

			this.getVerificationCode();
		},
		onUnload() {
			this.timer && clearInterval(this.timer);
		},
		methods: {
			async submit() {
				const { password, newPassword, repeatPassword } = this;

				if (newPassword !== repeatPassword) {
					uni.showToast({ title: "两次输入的新密码不一致，请重新输入", icon: "none" });
					return;
				}

				const request = {
					login: {
						url: "/Home/Login/fougot",
						params: {
							phone: this.phoneNumber,
							pwd: password,
							code: this.captcha,
						},
					},
					transaction: {
						url: "/Home/info/skjymm",
						params: {
							phone: this.phoneNumber,
							code: this.captcha,
							aq_pwd: repeatPassword,
						},
					},
				};

				const { msg: title } = await this.$request.post(request[this.type].url, params);

				await uni.showToast({ title, icon: "none", mask: true });
				await sleep(1.5);
				uni.navigateBack();
			},
			countDown() {
				const { codeBtn } = this;
				codeBtn.disabled = true;
				this.timer = setInterval(() => {
					codeBtn.count--;
					codeBtn.text = `${codeBtn.count}s后再试`;

					if (codeBtn.count <= 0) {
						codeBtn.disabled = false;
						codeBtn.text = "获取验证码";
						codeBtn.count = 60;
						this.timer && clearInterval(this.timer);
						this.timer = null;
					}
				}, 1000);
			},

			async getVerificationCode() {
				if (this.codeBtn.disabled) return;

				const { data } = await this.$request.get("/Home/info/me");

				this.phoneNumber = data.ue_account;

				this.$request
					.get("/Home/Login/xgzlmobile", {
						mobile: this.phoneNumber,
						type: "sms",
					})
					.then(({ msg: title }) =>
						uni.showToast({
							title,
							icon: "none",
							success: this.countDown,
						})
					);
			},
		},
	};
</script>

<style lang="scss">
	.smrz {
		padding: 30rpx;
		box-sizing: border-box;

		.tips {
			font-size: 24rpx;
			font-family: Alibaba PuHuiTi;
			font-weight: 400;
			color: #999999;
			line-height: 36rpx;
			margin-bottom: 30rpx;
		}

		.inputContent {
			border-radius: 10rpx;
			background: #ffffff;
			padding: 0 20rpx;
			margin-bottom: 150rpx;
			.list {
				display: flex;
				flex-direction: row;
				align-items: center;
				border-bottom: 1rpx solid #e5e5e5;
				padding: 30rpx;

				.leftText {
					width: 200rpx;
					font-size: 30rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: 400;
					color: #1a1a1a;
					margin-right: 10rpx;
				}

				input {
					flex: 1;
				}

				.pl {
					font-size: 24rpx;
					font-family: Alibaba PuHuiTi;
					font-weight: 400;
					color: #999999;
				}
			}
		}

		.submit {
			width: 100%;
			height: 80rpx;
			background: #2a62ff;
			border-radius: 10rpx;
			font-size: 34rpx;
			font-family: Alibaba PuHuiTi;
			font-weight: 400;
			color: #ffffff;
			line-height: 80rpx;
			text-align: center;
		}
	}
</style>
